package com.bootdo.fx.controller;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.bootdo.common.domain.Tree;
import com.bootdo.common.utils.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.fx.domain.GoodsCategoryDO;
import com.bootdo.fx.service.GoodsCategoryService;

import javax.servlet.http.HttpServletResponse;

/**
 * 
 * 
 * @author 袁飞龙
 * @email 1992lcg@163.com
 * @date 2017-11-30 17:12:20
 */
 
@Controller
public class GoodsCategoryController {
	@Autowired
	private GoodsCategoryService goodsCategoryService;
	@Autowired
	private IdWorker idWorker;

	@GetMapping("/fx/goodsCategory")
	@RequiresPermissions("fx:goodsCategory:goodsCategory")
	String GoodsCategory(){
	    return "fx/goodsCategory/goodsCategory";
	}
	
	@ResponseBody
	@GetMapping("/fx/goodsCategory/list")
	@RequiresPermissions("fx:goodsCategory:goodsCategory")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<GoodsCategoryDO> goodsCategoryList = goodsCategoryService.list(query);
		int total = goodsCategoryService.count(query);
		PageUtils pageUtils = new PageUtils(goodsCategoryList, total);
		return pageUtils;
	}
	
	@GetMapping("/fx/goodsCategory/add")
	@RequiresPermissions("fx:goodsCategory:add")
	String add(){
	    return "fx/goodsCategory/add";
	}

	@GetMapping("/fx/goodsCategory/edit/{categoryId}")
	@RequiresPermissions("fx:goodsCategory:edit")
	String edit(@PathVariable("categoryId") Long categoryId,Model model){
		GoodsCategoryDO goodsCategory = goodsCategoryService.get(categoryId);
		model.addAttribute("goodsCategory", goodsCategory);
	    return "fx/goodsCategory/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/fx/goodsCategory/save")
	@RequiresPermissions("fx:goodsCategory:add")
	public R save( GoodsCategoryDO goodsCategory) throws ParseException {
		goodsCategory.setCategoryId(idWorker.nextId()+"");
		goodsCategory.setCreateTime(DateUtils.parse(DateUtils.format(new Date(),DateUtils.DATE_TIME_PATTERN),DateUtils.DATE_TIME_PATTERN));
		if(goodsCategoryService.save(goodsCategory)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/fx/goodsCategory/update")
	@RequiresPermissions("fx:goodsCategory:edit")
	public R update( GoodsCategoryDO goodsCategory) throws ParseException {
		goodsCategory.setUpdateTime(DateUtils.parse(DateUtils.format(new Date(),DateUtils.DATE_TIME_PATTERN),DateUtils.DATE_TIME_PATTERN));
		goodsCategoryService.update(goodsCategory);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/fx/goodsCategory/remove")
	@ResponseBody
	@RequiresPermissions("fx:goodsCategory:remove")
	public R remove( Long categoryId){
		if(goodsCategoryService.remove(categoryId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/fx/goodsCategory/batchRemove")
	@ResponseBody
	@RequiresPermissions("fx:goodsCategory:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] categoryIds){
		goodsCategoryService.batchRemove(categoryIds);
		return R.ok();
	}
	@GetMapping("/fx/goodsCategory/treeView")
	String treeView() {
		return  "/fx/goodsCategory/categoryTree";
	}

	@GetMapping("/fx/goodsCategory/tree")
	@ResponseBody
	public Tree<GoodsCategoryDO> tree() {
		Tree<GoodsCategoryDO> tree = new Tree<GoodsCategoryDO>();
		tree = goodsCategoryService.getTree();
		return tree;
	}


	/*
 	* 或者分类列表
 	* */
	@RequestMapping("/wx/goodsCategory/getAllGoodsCategory")
	@ResponseBody
	public JsonResult getAllGoodsCategory(HttpServletResponse response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		return ResultUtil.renderSuccess(goodsCategoryService.list(null));
	}

}
